package com.cycling.app.activity;

import android.app.Dialog;
import android.content.Context;
import android.content.DialogInterface;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.view.MenuItem;
import android.view.View;
import android.view.Window;
import android.widget.TextView;

import com.cycling.app.R;
import com.cycling.app.bean.User;
import com.cycling.app.utils.DribblePrefs;

import java.util.Locale;
import java.util.Timer;
import java.util.TimerTask;

/**
 * Created by ZhouLei on 2017/5/6.
 */

public class BillingActivity extends BaseActivity {

    private TextView time,cost;
    private Timer timer;
    private TimerTask timerTask;
    private long cnt;

    @Override
    public int layoutResId() {
        return R.layout.activity_billing;
    }

    @Override
    public void onActivityCreate(@Nullable Bundle savedInstanceState) {
        setTitle(R.string.app_name);
        time = (TextView) findViewById(R.id.tv_time);
        cost = (TextView) findViewById(R.id.tv_cost);
        timer = new Timer();
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        if (item.getItemId() == android.R.id.home) {
            onBackPressed();
            return true;
        }
        return onCompatOptionsItemSelected(item);
    }

    @Override
    public void onAttachedToWindow() {
        super.onAttachedToWindow();
        startTimer();
    }

    @Override
    protected void onDestroy() {
        stopTimer();
        super.onDestroy();
    }

    @Override
    public void onBackPressed() {
        finishStroke(null);
    }

    public void startTimer() {
        timerTask = new TimerTask() {
            @Override
            public void run() {
                runOnUiThread(new Runnable() {
                    @Override
                    public void run() {
                        cost.setText(getStringCost(calcCost(cnt)));
                        time.setText(getStringTime(cnt++));
                    }
                });
            }
        };
        timer.schedule(timerTask, 0, 1000);
    }
    private double calcCost(long cnt) {
        double cost;
        int h = 60 * 60 * 1000;
        if (cnt == 0 || cnt < h) {
            cost = 0.d;
        } else {
            cost = cnt / h;
        }
        return cost;
    }
    private String getStringCost(double cost) {
        return String.format(Locale.CHINA, "当前费用：￥%.1f", cost);
    }

    private String getStringTime(long cnt) {
        long hour = cnt / 3600;
        long min = cnt % 3600 / 60;
        long second = cnt % 60;
        return String.format(Locale.CHINA, "%02d:%02d:%02d", hour, min, second);
    }

    public void stopTimer() {
        if (!timerTask.cancel()) {
            timerTask.cancel();
            timer.cancel();
        }
    }

    public void finishStroke(View view) {
        stopTimer();
        MySettlementDlg dlg = new MySettlementDlg(BillingActivity.this, calcCost(cnt), new MySettlementDlg.OnConfirmListener() {
            @Override
            public void onConfirm() {
                finish();
            }
        });
        dlg.show();
    }


    // 设置ip的Dialog
    static class MySettlementDlg extends Dialog {

        private OnConfirmListener confirmListener;
        private double cost;

        public MySettlementDlg(Context context, int theme) {
            super(context, theme);
        }

        public MySettlementDlg(Context context, double cost, OnConfirmListener confirmListener) {
            this(context, 0);
            requestWindowFeature(Window.FEATURE_NO_TITLE);
            setCancelable(false);
            this.confirmListener = confirmListener;
        }

        @Override
        public void setOnDismissListener(DialogInterface.OnDismissListener listener) {
            super.setOnDismissListener(listener);
        }

        @Override
        protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.dialog_settlement_view);
            TextView tv_cost = (TextView) findViewById(R.id.tv_cost);
            tv_cost.setText(String.format(Locale.CHINA, "￥%.1f", cost));
            findViewById(R.id.btn_settlement).setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    if (confirmListener != null) {
                        User user = DribblePrefs.get().getUser();
                        user.setMobile(String.valueOf(cost));
                        DribblePrefs.get().update(user);
                        confirmListener.onConfirm();
                    }
                }
            });
        }

        public interface OnConfirmListener {
            void onConfirm();
        }

    }
}
